package com.code.google.p.jyve.berekeningen;

class TweewaardigZwakkeBerekening extends ZwakkeBerekening {

	//TODO: versnellen berekening

	@Override
	public double berekenVanuitTitrant(double titrant) {
		//Kan alleen in benadering
		double pH = 7;		
		double volTot = (titrant + mVolumeOplossing)/1e3;
		titrant /= 1e3;

		if (mTypeStof == ZUUR){
			//afslopen van 1e H-tje
			if (mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
				double a = 1;
				double b = -mMolariteitTitrant * titrant + mVolumeOplossing/1e3 * 1e-7 + volTot * mKz[0];
				double c = -mKz[0] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

				double x = abcMax(a, b, c);
				
				pH = -Math.log10((mVolumeOplossing/1e3 * 1e-7 + x - mMolariteitTitrant * titrant)/volTot);
				//System.out.println(pH);
			}
			else{
				//afslopen 2e H-tje
				if (2 * mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
					double a = 1; 
					double b = -mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3 + mKz[1]*volTot;
					double c = -mKz[1] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

					double x = abcMax(a, b, c);
					pH = -Math.log10((x  - mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
					//System.out.println(pH);
				}
				//na afslopen H-tjes
				else{
					double kb = kzToKb(mKz[1]);

					double a = 1;
					double b = mMolariteitTitrant * titrant - 2 * mMolariteitOplossing * mVolumeOplossing/1e3 + volTot * kb;
					double c = -kb * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

					double x = abcMax(a, b, c);

					pH = -Math.log10(KW) + Math.log10((x + mMolariteitTitrant * titrant - 2 * mMolariteitOplossing * mVolumeOplossing/1e3)/volTot);
					//System.out.println(pH);
				}
			}

		}
		else{
			if (mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
				double a = 1;
				double b = -mMolariteitTitrant * titrant + mVolumeOplossing/1e3 * 1e-7 + volTot * mKz[0];
				double c = -mKz[0] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;
				
				double x = abcMax(a, b, c);
				
				pH = -Math.log10(KW) + Math.log10((x  - mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
			}else{
				if (2 * mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
					double a = 1; 
					double b = -mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3 + mKz[1]*volTot;
					double c = -mKz[1] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

					double x = abcMax(a, b, c);
					pH = -Math.log10(KW) + Math.log10((x  - mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
					
				}else{
					double kz = kzToKb(mKz[1]);

					double a = 1;
					double b = mMolariteitTitrant * titrant - 2 * mMolariteitOplossing * mVolumeOplossing/1e3 + volTot * kz;
					double c = -kz * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

					double x = abcMax(a, b, c);

					pH = -Math.log10((x + mMolariteitTitrant * titrant - 2 * mMolariteitOplossing * mVolumeOplossing/1e3)/volTot);
				}
			}
		}

		return pH;
	}

}
